/*
 * @(#)ExceptionHandler.java
 *
 * Copyright 2011 Xinhua Online, Inc. All rights reserved.
 */

package com.winxuan.channel.support.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.winxuan.channel.model.object.ChannelResult;


/**
 * description
 * 
 * @author liuan
 * @version 1.0,2011-2-24
 */
public class ExceptionHandler implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(request.getRequestURI());
		modelAndView.addObject(new ExceptionResponse(ex.getMessage()));
		request.setAttribute(EXCEPTION, ex);
//		modelAndView.setViewName("/result");
		ChannelResult channelResult = new ChannelResult();
		channelResult.setSuccess(false);
		channelResult.setMsg(ex.getMessage());
		modelAndView.addObject("channelResult",channelResult);
		LOG.error(ex.getMessage());
		return modelAndView;
	}

	public static final String EXCEPTION = "exception";
	private static final Log LOG = LogFactory.getLog(ExceptionHandler.class);
}
